package ca.uvic.cs.cogz.mapping.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

import ca.uvic.cs.chisel.cajun.util.GradientPanel;

public class NotificationWindow extends JFrame {
	private static final long serialVersionUID = -837471184010718767L;

	private JFrame parentFrame;
	private String title;
	private String message;
	
	private Timer fadeInTimer;
	private Timer fadeOutTimer;
	
	private int x;
	private int y;
	private int startLocY;
	
	public NotificationWindow(JFrame parentFrame, String title, String message) {
		this.parentFrame = parentFrame;
		this.title = title;
		this.message = message;
	}
	
	public int getStartLocY() {
		return startLocY;
	}
	
	public void display(int startLocY) {
		this.startLocY = startLocY;
		x = (parentFrame.getLocationOnScreen().x + parentFrame.getWidth()) - 250;
		y = startLocY;
		
		this.setLocation(x, y);
		init();
		
		this.setVisible(true);
		
		fadeInTimer = new Timer(50, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int height = getHeight() + 5;
				if(height <= 100) {
					setLocation(x, y - height);
					setSize(new Dimension(getWidth(), height));
					getContentPane().repaint();
				} else if(fadeInTimer.getInitialDelay() == 50) {
						fadeInTimer.setInitialDelay(5000);
						fadeInTimer.restart();
				}
				else {
					y = getY();
					fadeInTimer.stop();
					fadeOutTimer.start();
				}
			}
		});
		fadeInTimer.setRepeats(true);
		fadeInTimer.start();
		
		fadeOutTimer = new Timer(50, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int height = getHeight() - 5;
				if(height >= 0) {
					setLocation(x, y + (100 - height));
					setSize(new Dimension(getWidth(), height));
					getContentPane().repaint();
				} else {
					fadeOutTimer.stop();
					setVisible(false);
					dispose();
				}
			}
		});
		fadeOutTimer.setRepeats(true);
	}
	
	private void init() {
		this.setUndecorated(true);
		this.setAlwaysOnTop(true);
		this.setSize(250, 0);
		
		JPanel mainPanel = new JPanel(new BorderLayout());
		mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		this.getContentPane().add(mainPanel, BorderLayout.CENTER);
		
		GradientPanel titlePanel = new GradientPanel();
		JLabel titleLabel = new JLabel("<html>" + title + "</html>");
		titlePanel.add(titleLabel);
		titleLabel.setFont(titleLabel.getFont().deriveFont(14f).deriveFont(Font.BOLD));
		mainPanel.add(titlePanel, BorderLayout.NORTH);
		
		JEditorPane editPane = new JEditorPane();
		editPane.setEditable(false);
		editPane.setOpaque(false);
		editPane.setFont(editPane.getFont().deriveFont(12f));
		
		editPane.setText(message);
		GradientPanel messagePanel = new GradientPanel(new BorderLayout());
		messagePanel.setGradientColors(GradientPanel.BG_START, Color.white);
		messagePanel.add(editPane, BorderLayout.CENTER);
		
		messagePanel.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				setVisible(false);
				dispose();
			}
		});
		
		mainPanel.add(messagePanel, BorderLayout.CENTER);
	}
}
